﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Command.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   Defines the CommandBase type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace mPassRT.Common
{
    using System;
    using System.Windows.Input;

    /// <summary>The command.</summary>
    public class Command : ICommand
    {
        #region Public Events

        /// <summary>The can execute changed.</summary>
        public event EventHandler CanExecuteChanged;

        #endregion

        #region Public Properties

        /// <summary>Gets or sets the can execute handler.</summary>
        public Func<object, bool> CanExecuteHandler { get; set; }

        /// <summary>Gets or sets the execute handler.</summary>
        public Action<object> ExecuteHandler { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>The can execute.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteHandler != null)
            {
                return this.CanExecuteHandler(parameter);
            }

            return true;
        }

        /// <summary>The execute.</summary>
        /// <param name="parameter">The parameter.</param>
        public void Execute(object parameter)
        {
            if (this.ExecuteHandler != null)
            {
                this.ExecuteHandler(parameter);
            }
        }

        /// <summary>The on can execute changed.</summary>
        public void OnCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(this, new EventArgs());
            }
        }

        #endregion
    }
}